<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { reactive, watch } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
// import { FormSchema } from '@/types/form'
import { useI18n } from '@/hooks/web/useI18n'

const { required } = useValidator()

const { t } = useI18n()
const props = defineProps({
  currentRow: {
    type: Object,
    default: () => null
  }
  // formSchema: {
  //   type: Array as PropType<FormSchema[]>,
  //   default: () => []
  // }
})

const rules = reactive({
  roleCode: [required()],
  roleName: [required()]
})

const { register, methods, elFormRef } = useForm({
  schema: [
    {
      field: 'roleCode',
      label: t('RoleManage.roleCode'),
      component: 'Input',
      colProps: {
        span: 24
      }
    },
    {
      field: 'roleName',
      label: t('RoleManage.roleName'),
      component: 'Input',
      colProps: {
        span: 24
      }
    },
    {
      field: 'description',
      label: t('RoleManage.description'),
      component: 'Input',
      colProps: {
        span: 24
      },
      componentProps: {
        type: 'textarea',
        rows: 2
      }
    }
  ]
})

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    const { setValues } = methods
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register" />
</template>
